﻿@{
    ViewData["Title"] = "角色列表";
}
<div class="layui-layout layui-layout-admin">
    <div class="layui-card">
        <div class="layui-card-body">
            <form class="layui-form layui-form-pane">
                <div class="layui-form-item">
                    <label class="layui-form-label">业务名称</label>
                    <div class="layui-input-inline">
                        <input type="text" name="busName" autocomplete="off" class="layui-input">
                    </div>
                    <label class="layui-form-label">数据库表名</label>
                    <div class="layui-input-inline">
                        <input type="text" name="tableName" autocomplete="off" class="layui-input">
                    </div>
                    <div class="layui-inline">
                        <button type="submit" class="layui-btn" lay-submit lay-filter="btnsearch">
                            <i class="layui-icon layui-icon-search"></i> 查询
                        </button>
                    </div>
                </div>
            </form>

            <table id="lsttable" lay-filter="lsttable"></table>
            <script type="text/html" id="toolbar">
                <button class="layui-btn layui-btn-sm" lay-event="add">
                    <i class="layui-icon layui-icon-addition"></i> 添加
                </button>
            </script>
            <script type="text/html" id="bar">
                <a class="layui-btn layui-btn-xs layui-btn-primary layui-border" lay-event="edit">编辑</a>
                <a class="layui-btn layui-btn-xs layui-btn-primary layui-border" lay-event="del">删除</a> |
                <a class="layui-btn layui-btn-xs layui-bg-red" lay-event="runLocal">生成代码</a>
                <a class="layui-btn layui-btn-xs layui-bg-blue" lay-event="config">配置</a>     
                <a class="layui-btn layui-btn-xs layui-bg-blue" lay-event="formDesigner">表单设计</a> |
                <a class="layui-btn layui-btn-xs layui-bg-blue" lay-event="formRun">测试</a>
            </script>
        </div>
    </div>
</div>

@section scripts {
    <script type="text/javascript">
        var app = {};
        layui.use(['tools', 'toast', 'http'], function () {
            var table = layui.table,
                $ = layui.$,
                form = layui.form,
                http = layui.http,
                toast = layui.toast,
                tools = layui.tools;

            app.init = function () {
                //
                form.on('submit(btnsearch)', function (data) {
                    console.log(data.field)
                    table.reload('lsttable', {
                        where: data.field
                    })
                    return false;
                });
                //
                let cols =
                    [[
                        { field: 'LAY_NUM', title: '序号', width: 40 },
                        { title: '库定位器', field: 'configId', width: 140 },
                        { title: '表名称', field: 'tableName', minWidth: 160 },
                        { title: '业务名称', field: 'busName', width: 160 },
                        { title: '命名空间', field: 'nameSpace', width: 200 },
                        { title: '生成方式', field: 'generateType', width: 100 },
                        { fixed: 'right', title: '操作', toolbar: '#bar', align: 'right', width: 400 }
                    ]];
                //
                tools.PageTable({ url: '/api/sysCodeGen/page', cols });
                table.on('toolbar(lsttable)', function (obj) {
                    var id = obj.config.id;
                    var checkStatus = table.checkStatus(id);
                    switch (obj.event) {
                        case 'add':
                            app.add();
                            break;
                    };
                });
                table.on('tool(lsttable)', function (obj) {
                    var data = obj.data;
                    switch (obj.event) {
                        case 'runLocal':
                            app.runLocal(data);
                            break;
                        case 'edit':
                            app.add(data);
                            break;
                        case 'config':
                            app.config(data);
                            break;
                        case 'del':
                            app.del(data.id);
                            break;
                        case 'formDesigner':
                            parent.changePage({
                                id: 'formDesigner_' + data.id,
                                title: data.busName + '表单设计',
                                url: '/form/formDesigner/' + data.id,
                                close: true
                            })
                            break;
                        case 'formRun':
                            parent.changePage({
                                id: 'formRun_' + data.id,
                                title: data.busName + '管理',
                                url: '/form/formList/' + data.id,
                                close: true
                            })
                            break;
                    };
                });
            }();
            //
            app.add = function (data) {
                data = data || {};
                tools.open({
                    area: ['700px', '100%'],
                    content: './add',
                    title: '管理',
                    success: function (layero, index, that) {
                      tools.initValue(layero, data);
                    },
                    yes: function (index, layero) {
                        var iframeWin = window[layero.find('iframe')[0]['name']];
                        iframeWin.app.save();
                    }
                });
            }
            app.config = function (data) {
                data = data || {};
                tools.open({
                    area: ['100%', '100%'],
                    maxmin: true,
                    content: './config',
                    title: '配置',
                    success: function (layero, index, that) {
                        tools.initValue(layero, data);
                    },
                    yes: function (index, layero) {
                        var iframeWin = window[layero.find('iframe')[0]['name']];
                        iframeWin.app.save();
                    }
                });
            }
            app.runLocal = function (data) {
                var index = tools.confirm('确定要生成吗？', function () {
                    http.post('/api/sysCodeGen/runLocal', data).then((res) => {
                        if (res.code == 200) {
                            layer.close(index);
                            toast.success({ message: '生成成功！' });
                            //
                            window.open(res.result.url);
                        }
                        else {
                            layer.alert('生成失败，错误信息' + res.message, { icon: 2 });
                        }
                    })
                });
            }
            app.del = function (id) {
                var index = tools.confirm('您确定要删除当前记录吗？', function () {
                    http.post('/api/sysCodeGen/delete', [{ id }]).then((res) => {
                        if (res.code == 200) {
                            layer.close(index);
                            table.reload('lsttable');
                            toast.success({ message: '删除成功！' });
                        }
                        else {
                            layer.alert('删除失败，错误信息' + res.message, { icon: 2 });
                        }
                    })
                });
            }
        })
    </script>
}
